﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using log4net;
using TreeBuilderSite.Models;

namespace TreeBuilderSite.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILog _lg = LogManager.GetLogger(typeof (HomeController));

        private readonly List<Example> _examples =
            new List<Example>
                {
                    new Example
                        {
                            Description =
                                "Generative grammar tree from book \"Введение в генеративную грамматику\" (O. Mitrenina, E. Romanova, N. Sloussar)",
                            ImageUrl = "example_1.png",
                            TreeFileUrl = "example_1.tree"
                        },
                    new Example
                        {
                            Description =
                                "Generative grammar tree",
                            ImageUrl = "example_2.png",
                            TreeFileUrl = "example_2.tree"
                        },
                    new Example
                        {
                            Description = "Generative grammar tree from book \"На стыке теорий\" (N. Sloussar)",
                            ImageUrl = "example_3.png",
                            TreeFileUrl = "example_3.tree"
                        },
                    new Example
                        {
                            Description = "Semantic tree from Barbara Partee's handout",
                            ImageUrl = "example_4.png",
                            TreeFileUrl = "example_4.tree"
                        }
                };


        public ActionResult Index()
        {
            LogAction("Index");
            return View();
        }

        private void LogAction(string index)
        {
            string ip = Request.UserHostAddress;
            string agent = Request.UserAgent;
            string languages = Request.UserLanguages == null ? "" : string.Join(";", Request.UserLanguages);
            string referrer = Request.UrlReferrer == null ? "" : Request.UrlReferrer.ToString();

            Task.Factory.StartNew(
                () =>_lg.Info(index 
                    + "\tIP: " + ip 
                    + "\tAgent: " + agent 
                    + "\tLanguages: " + languages 
                    + "\tReferrer: " + referrer));

        }

        public ActionResult Examples()
        {
            LogAction("Examples");
            return View(_examples);
        }

        public ActionResult Download()
        {
            LogAction("Download");
            return View();
        }

        public ActionResult DownloadInstaller()
        {
            LogAction("DownloadInstaller");
            string filename = Url.Content("~/Content/TreeBuilder setup.exe");
            return File(filename, "application/octet-stream", "TreeBuilder setup.exe");
        }

        public ActionResult DownloadSourceCode()
        {
            LogAction("DownloadSourceCode");
            return new RedirectResult("http://treebuilder.codeplex.com/");
        }

        public ActionResult Images(string id)
        {
            string url = Url.Content("~/Images/" + id);
            return File(url, "images/png");
        }

        public ActionResult Trees(string id)
        {
            string url = Url.Content("~/Trees/" + id);
            return File(url, "application/treebuilder");
        }

        public ActionResult Contacts()
        {
            LogAction("Contacts");
            return View();
        }

        public ActionResult AboutAuthor()
        {
            LogAction("AboutAuthor");
            return View();
        }

        public ActionResult Version()
        {
            LogAction("Version");
            return View();
        }

    }
}
